package com.sanhe.a448;

import android.app.Activity;
import android.util.Log;
import android.view.Gravity;

import com.menu.app.MyApp;
import com.menu.app.activity.base.MenuBaseActivity;
import com.menu.app.activity.base.PortBaseActivity;
import com.menu.app.event.BlockEvent;
import com.menu.app.it.Reset;
import com.menu.app.model.Models;
import com.menu.app.util.Constant;
import com.menu.app.util.Pdf;
import com.menu.app.util.pipe.BasePortEventPipe;
import com.menu.app.util.port.SerialHandle;
import com.menu.app.util.port.SerialHandler;
import com.menu.app.util.port.SerialInterface;
import com.plata.base.aclr.utils.SPHelper;
import com.sanhe.a448.activity.BoardActivity;
import com.sanhe.a448.activity.CustomInfoActivity;
import com.sanhe.a448.activity.SettingMenuActivity;
import com.sanhe.a448.activity.work.ModeActivity;
import com.sanhe.a448.adapter.MyBootAdapter;
import com.sanhe.a448.bean.WorkParam;
import com.sanhe.a448.it.WorkingPage;
import com.sanhe.a448.utils.MySerialHandler;
import com.sanhe.a448.utils.PortPipe;
import com.sanhe.a448.view.MyPopMenu;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class App extends MyApp {

    static App sInstance;

    @Override
    public String getUmId() {
        return "";
    }

    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
        Constant.PRODUCT = BuildConfig.PRODUCT;
        if (isDefault()) {
            Constant.degree = 0;

            Constant.MENU_GRAVITY = Gravity.BOTTOM;
            MenuBaseActivity.popMenuClass = MyPopMenu.class;

            Constant.BOOT_ADAPTER = MyBootAdapter.class.getName();

            EventBus.getDefault().register(this);
            boolean bo = SPHelper.getInstance(this).getBoolean("rent", false);
            Models.Companion.get().getRentEvent().setValue(bo ? 1 : 0);
        }
    }

    @Override
    public void back2Main(MenuBaseActivity.JumpPrepareListener listener) {
        ignore = true;
        Iterator<Activity> it = list.iterator();
        while (it.hasNext()) {
            Activity activity = it.next();
            if (activity instanceof PortBaseActivity && !isWorkingPage(activity)) {
                ((PortBaseActivity) activity).exit();
            }
        }
        if (listener != null) {
            listener.onPrepared();
        }
    }

    @Override
    public Reset getReset() {
        return null;
    }

    @Override
    protected void toBackground() {
    }

    @Override
    public boolean isWorkingPage() {
        Activity ac = MyApp.getApp().getTopActivity();
        return isWorkingPage(ac);
    }

    @Override
    public boolean isWorkingPage(Activity activity) {
        return activity instanceof WorkingPage;
    }

    public boolean isPwd() {
        return false;
    }

    @Override
    public Class getMainClass() {
        return ModeActivity.class;
    }

    public static App getApp() {
        return sInstance;
    }

    @Override
    public Map getPageMap() {
        HashMap<String, Class> router = new HashMap<>();
        router.put("setting", SettingMenuActivity.class);
        return router;
    }

    @Override
    public int getRate() {
        return 19200;
    }

    @Override
    public Class getBoard() {
        return BoardActivity.class;
    }

    @Override
    public BasePortEventPipe getPortPipe() {
        return PortPipe.getInstance();
    }

    @Override
    public Class getSettingClass() {
        return SettingMenuActivity.class;
    }

    @Override
    public Class getCustomInfoClass() {
        return CustomInfoActivity.class;
    }

    @Override
    public Pdf getPdfCreator() {
        return null;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(BlockEvent event) {
        Log.e("tagg", "block block block, " + event.getValue());
//        SerialHandle.send.clear();
    }

    MySerialHandler serialHandler;

    @Override
    public SerialHandler getSerialHandler(int type) {
        if (type == 1) {
            return null;
        }
        if (serialHandler == null) {
            serialHandler = new MySerialHandler();
        }
        return serialHandler;
    }

    private WorkParam workParam = null;

    public void setWorkParam(WorkParam param) {
        workParam = param;
    }

    public WorkParam getWorkParam() {
        return workParam;
    }

    @Override
    public SerialInterface getSerialInterface(int type) {
        return new SerialHandle(getSendType(), 1000, TimeUnit.MICROSECONDS);
    }
}
